package api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import domain.User;
import exception.ProductException;
import service.ProductService;
import service.UserService;
import utils.GetRequestJsonUtils;
import utils.Response;
import utils.ResponseCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class WeekHotBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        三步走，第一步获取输入，第二步调用service进行业务处理，第三步向浏览器输出返回消息
        Response build=null;
        ProductService productService=new ProductService();
        try {
            List<Object[]> hotBooks = productService.getWeekHotProduct();
            build=Response.build(ResponseCode.SUCCESS,hotBooks);
        }catch (ProductException e){
            e.printStackTrace();
            build=Response.build(e.getResponseCode());
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSON(build).toString());
    }
}
